<?php

class StudentInfo_Model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * @param $row
     * @return StudentInfo
     */
    public static function getStudentInfoByRow($row)
    {
        $thatStudentInfo = new StudentInfo;
        $thatStudentInfo->setId($row->id);
        $thatStudentInfo->setStudentNum($row->student_num);
        $thatStudentInfo->setStudentName($row->student_name);
        $thatStudentInfo->setGrade($row->grade);
        $thatStudentInfo->setMajor($row->major);
        $thatStudentInfo->setUserId($row->user_id);
        $thatStudentInfo->setSchoolId($row->school_id);
        return $thatStudentInfo;
    }

    /**
     * @param StudentInfo $thatStudentInfo
     * @return array
     */
    public static function getModelByStudentInfo($thatStudentInfo)
    {
        return array(
            'student_num'  => $thatStudentInfo->getStudentNum(),
            'student_name' => $thatStudentInfo->getStudentName(),
            'grade'        => $thatStudentInfo->getGrade(),
            'major'        => $thatStudentInfo->getMajor(),
            'user_id'      => $thatStudentInfo->getUserId(),
            'school_id'    => $thatStudentInfo->getSchoolId(),
        );
    }

    /**
     * @param int $id
     * @return StudentInfo
     */
    public static function getStudentInfoById($id)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->select('student_info.*');
        $CI->db->from('student_info');
        $CI->db->where('student_info.id', $id);

        $query = $CI->db->get();
        $row   = $query->row();
        if ($row)
            return self::getStudentInfoByRow($row);
        else
            return null;
    }

    /**
     * @param int $userId
     * @return StudentInfo
     */
    public static function getStudentInfoByUserId($userId)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->select('student_info.*');
        $CI->db->from('student_info');
        $CI->db->where('student_info.user_id', $userId);

        $query = $CI->db->get();
        $row   = $query->row();
        if ($row)
            return self::getStudentInfoByRow($row);
        else
            return null;
    }
    /**
     * @param bool|int $limit
     * @param bool|int $offset
     * @return array
     */
    public static function getStudentInfoArray($limit = false, $offset = false)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->select('student_info.*');
        $CI->db->from('student_info');
        $CI->db->order_by('id', 'desc');

        if ($limit !== false && is_numeric($limit)) {
            if ($offset === false || !is_numeric($offset))
                $offset = 0;
            $CI->db->limit($limit, $offset);
        }

        $query  = $CI->db->get();
        $result = $query->result();

        $array = array();
        foreach ($result as $row) {
            $array[] = self::getStudentInfoByRow($row);
        }
        return $array;
    }

    /**
     * @param int $schoolId
     * @param string $studentNum
     * @return bool
     */
    public static function isSchoolIdAndStudentNumExist($schoolId, $studentNum)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->select('student_info.id');
        $CI->db->from('student_info');
        $CI->db->where('school_id', $schoolId);
        $CI->db->where('student_num', $studentNum);
        if ($CI->db->get()->num_rows())
            return true;
        else
            return false;
    }

    /**
     * @param StudentInfo $thatStudentInfo
     * @throws DatabaseException
     * @return StudentInfo
     */
    public static function add($thatStudentInfo)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $result = $CI->db->insert('student_info', self::getModelByStudentInfo($thatStudentInfo));
        if ($result)
            return self::getStudentInfoById($CI->db->insert_id());
        else
            throw new DatabaseException('插入数据失败');
    }

    /**
     * @param StudentInfo $thatStudentInfo
     * @throws DatabaseException
     * @return StudentInfo
     */
    public static function update($thatStudentInfo)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->where('id', $thatStudentInfo->getID());
        $result = $CI->db->update('student_info', self::getModelByStudentInfo($thatStudentInfo));
        if ($result)
            return self::getStudentInfoById($thatStudentInfo->getID());
        else
            throw new DatabaseException('更新数据失败');
    }

    /**
     * @param StudentInfo $thatStudentInfo
     * @throws DatabaseException
     * @return bool
     */
    public static function delete($thatStudentInfo)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->where('id', $thatStudentInfo->getID());
        $result = $CI->db->delete('student_info');
        if ($result)
            return true;
        else
            throw new DatabaseException('删除数据失败');
    }

}
